﻿NcodeImageResizer.BBURL = APP.URL + '/images';
NcodeImageResizer.MODE = 'newwindow'; //'enlarge';
NcodeImageResizer.MAXWIDTH = '670';
NcodeImageResizer.MAXHEIGHT = '0';
vbphrase = new Array();
vbphrase['ncode_imageresizer_warning_small'] = 'คลิกที่นี่เพื่อดูรูปภาพขนาดเต็ม';
vbphrase['ncode_imageresizer_warning_filesize'] = 'รูปภาพนี้ถูกย่อจากขนาดต้นฉบับ %1$sx%2$s. คลิกที่นี่เพื่อดูรูปภาพขนาดเต็ม';
vbphrase['ncode_imageresizer_warning_no_filesize'] = 'รูปภาพนี้ถูกย่อจากขนาดต้นฉบับ %1$sx%2$s. คลิกที่นี่เพื่อดูรูปภาพขนาดเต็ม';
vbphrase['ncode_imageresizer_warning_fullsize'] = 'คลิกที่นี่เพื่อดูรูปภาพขนาดย่อ';

window.addEvent('domready', function(){	
	if( $$('.inlined + .input-text') ){
		$$('.inlined + .input-text').each(function(el){
			if(el.value != ''){
				el.getPrevious('label.inlined').addClass('has-text').removeClass('focus');
			}
			el.addEvent('focus', function(){
				el.getPrevious('label.inlined').addClass('focus');
			});
			el.addEvent('keypress', function(){
				el.getPrevious('label.inlined').addClass('has-text').removeClass('focus');
			});
			el.addEvent('blur', function(){
				if(el.value == ''){
					el.getPrevious('label.inlined').removeClass('has-text').removeClass('focus');
				}
			});
		});
	}
	
	if( $('emo-toggle') ){
		var emo_box = $('emo-box');
		$('emo-toggle').addEvent('click', function(){
			if( emo_box.style.display == 'block' )
				emo_box.style.display = 'none';
			else
				emo_box.style.display = 'block';
		});
	}
});

function setNotification(msg){
	var bar = new Element('div', {
		'id': 'notification',
		'html': msg,
		'styles': {
			'position': "fixed",
			'top': "-40px",
			'left': "0px",
			'height': "20px",
			'width': "100%",
			'border': "1px solid #cfcfcf",
			'background-color': "#fdfdfd",
			'padding': "5px",
			'font-size': "16px",
			'font-weight': "bold",
			'text-align': "center",
			'z-index': 999999
		}
	});
	
	$(document.body).adopt(bar);
	bar.tween('top', '0');
	setTimeout(function(){
		bar.tween('top', '-40');
	}, 5000);
}

function setConfirm(el, ajax, callback){
	var id_el = el.href + '-confirm';
	var body = $(document.body);
	if(body.hasChild(id_el)) 
		return false;
	var div = new Element('div', {
		'id': id_el,
		'class': 'rounded confirm-box',
		'styles': {
			'top': parseInt(el.getPosition().y + el.getHeight() + 2),
			'left': parseInt(el.getPosition().x + el.getWidth() + 2),
			'position': 'absolute',
			'border': '1px solid #cfcfcf',
			'background-color': '#fdfdfd',
			'padding': '10px',
			'text-align': 'center',
			'z-index': '999999'
		}
	}).adopt(
		new Element('div', {
			'html': 'คุณแน่ใจแล้วใช่หรือไม่',
			'styles': {
				'font-size': '13px',
				'font-weight': 'bold',
				'padding-bottom': '10px'
			}
		}),
		new Element('a', {
			'class': 'small awesome',
			'text': 'ใช่',
			'events': {
				'click': function(e){
					e.stop();
					if(ajax){
						new Request.JSON({
							url: el.href,
							onSuccess: function(dt){
								if(typeof callback == 'function'){
									callback.call();
								}
							}
						}).get();
						div.dispose();
						return false;
					}
					window.location.href = el.href;
				}
			}
		}),
		new Element('a', {
			'class': 'small awesome',
			'text': 'ไม่ใช่',
			'events': {
				'click': function(){
					div.dispose();
				}
			}
		})
	);
	body.adopt(div);
	return false;
}

function preview_init(btnEL, inputEL, outputEL, session){
	$(btnEL).addEvent('click', function(e){
		e.stop();
		var text = $(inputEL).value.trim();
		new Request.JSON({
			url: APP.URL + '/ajax',
			onSuccess: function(dt){
				if(dt.status == 'success'){
					$(outputEL).getChildren('.body').set('html', dt.html);
				}
			}
		}).post({id: 1, action: 'preview', type: 'topic', session: session, text: text});
	});		
}

function quote_init(session){
	$$('.quote-link').each(function(el){
		el.addEvent('click', function(){
			var sep = el.getProperty('id').split('-');
			var action = sep[0];
			var type = sep[1];
			var id = sep[2];
			if(!action || !type || !id)
				return false;
			new Request.JSON({
				url: APP.URL + '/ajax',
				onSuccess: function(json){
					if(!json) return false;
					var quote_text = '';
					if(type == 'topic'){
						quote_text = json.topic_text;
					}else if(type == 'post'){
						quote_text = json.post_text;
					}
					$('post_text').value += '[quote]' + quote_text + '[/quote]';
					new Fx.Scroll(window).toElement('replytopic');
				}
			}).post({'id': id, 'type': type, 'action': action, 'session': session});
		});
	});
}

function gift_init(session){
	$$('.gift-link').each(function(el){
		el.addEvent('click', function(e){				
			var sep = el.getProperty('id').split('-');
			var action = sep[0];
			var type = sep[1];
			var id = sep[2];
			if(!action || !type || !id)
				return false;
			new Request.JSON({
				url: APP.URL + '/ajax',
				onSuccess: function(json){
					if(json.status =='error')
						return alert(json.message);
					el.set('text', 'ให้กิ้ฟท์เรียบร้อย');
					$('gifted-' + type + '-' + id).set('html', json.message);
				}
			}).post({'id': id, 'type': type, 'action': action, 'session': session});
		});
	});
}

function toggleBuddy(el, session){
	var id = el.getProperty('id');
	new Request.JSON({
		url: APP.URL + '/profile',
		onSuccess: function(dt){
			if(dt.status == 'error')
				return alert(dt.message);
			if(dt.status == 'success'){
				setNotification(dt.message);
				if(dt.type == 'isbuddy'){
					el.set('html', '<img src="' + APP.URL + '/images/remove-buddy.png" border="0" align="top" /> เลิกเป็นเพื่อน</a>');
				}
				else if(dt.type == 'unbuddy'){
					el.set('html', '<img src="' + APP.URL + '/images/add-buddy.png" border="0" align="top" /> เพิ่มเป็นเพื่อน</a>');
				}
			}
		}
	}).post({action: 'togglebuddy', id: id, session: session});
}
					
function replaceText(text, textarea)
{
	if(typeof(textarea.caretPos) != "undefined" && textarea.createTextRange)
	{
		var caretPos = textarea.caretPos;
		caretPos.text = caretPos.text.charAt(caretPos.text.length-1) == ' ' ? text + ' ' : text;
		caretPos.select();
	}
	else if(typeof(textarea.selectionStart) != "undefined")
	{
		var begin = textarea.value.substr(0, textarea.selectionStart);
		var end = textarea.value.substr(textarea.selectionEnd);
		var scrollPos = textarea.scrollTop;
		textarea.value = begin + text + end;
		if(textarea.setSelectionRange)
		{
			textarea.focus();
			textarea.setSelectionRange(begin.length + text.length, begin.length + text.length);
		}
		textarea.scrollTop = scrollPos;}
	else
	{
		textarea.value += text;
		textarea.focus(textarea.value.length-1);
	}
}

function surroundText(text1, text2, textarea)
{
	if(typeof(textarea.caretPos) != "undefined" && textarea.createTextRange)
	{
		var caretPos = textarea.caretPos, temp_length = caretPos.text.length;
		caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? text1 + caretPos.text + text2 + ' ' : text1 + caretPos.text + text2;
		if(temp_length == 0)
		{
			caretPos.moveStart("character", -text2.length);
			caretPos.moveEnd("character", -text2.length);
			caretPos.select();
		}
		else
			textarea.focus(caretPos);
	}
	else if(typeof(textarea.selectionStart) != "undefined")
	{
		var begin = textarea.value.substr(0, textarea.selectionStart);
		var selection = textarea.value.substr(textarea.selectionStart, textarea.selectionEnd - textarea.selectionStart);
		var end = textarea.value.substr(textarea.selectionEnd);
		var newCursorPos = textarea.selectionStart;
		var scrollPos = textarea.scrollTop;
		textarea.value = begin + text1 + selection + text2 + end;
		if(textarea.setSelectionRange)
		{
			if(selection.length == 0)
				textarea.setSelectionRange(newCursorPos + text1.length, newCursorPos + text1.length);
			else
				textarea.setSelectionRange(newCursorPos, newCursorPos + text1.length + selection.length + text2.length);
			textarea.focus();
		}
		textarea.scrollTop = scrollPos;
	}
	else
	{
		textarea.value += text1 + text2;
		textarea.focus(textarea.value.length - 1);
	}
}